package cn.xeblog.plugin.action.handler.command;

import cn.hutool.core.util.StrUtil;
import cn.xeblog.commons.entity.WeatherDTO;
import cn.xeblog.commons.enums.Action;
import cn.xeblog.commons.enums.WeatherType;
import cn.xeblog.commons.util.ParamsUtils;
import cn.xeblog.plugin.action.ConsoleAction;
import cn.xeblog.plugin.action.MessageAction;
import cn.xeblog.plugin.annotation.DoCommand;
import cn.xeblog.plugin.enums.Command;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * 天气命令处理器
 *
 * @author anlingyi
 * @date 2020/8/19
 */
@DoCommand(Command.WEATHER)
public class WeatherCommandHandler extends AbstractCommandHandler {

    /**
     * 配置选项
     */
    @Getter
    @AllArgsConstructor
    private enum Config {
        /**
         * 查询几天
         */
        DAY("-d");

        private String key;

        /**
         * 通过key值配置
         *
         * @param name 待匹配的名称
         * @return 相关配置选项
         */
        public static Config getConfig(String name) {
            return Arrays.stream(values()).filter(value -> value.getKey().equals(name)).findFirst().orElse(null);
        }
    }

    @Override
    public void process(String[] args) {

        int len = args.length;
        // 查看的天气地点
        String location = null;
        if (len > 0) {
            String name = args[0];
            if (Config.getConfig(name) == null) {
                location = name;
            }
        }
        location = StrUtil.trim(location);

        // 地名非空
        if (StrUtil.isBlank(location)) {
            ConsoleAction.showSimpleMsg("地名不能为空！");
            return;
        }

        // 查询天数参数为空时，默认查询当前温度
        String day = ParamsUtils.getValue(args, Config.DAY.getKey());
        if (StrUtil.isBlank(day)) {
            day = "0";
        }

        // 发送查询天气的消息，并显示提示
        WeatherDTO weather = new WeatherDTO(WeatherType.build(day), location);
        MessageAction.send(weather, Action.WEATHER);
        ConsoleAction.showSimpleMsg("正在获取天气信息，请耐心等待...");
    }

}
